package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.servcie.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;

/**
 * 处理和商家相关的接口功能
 */
@Slf4j
@RestController
@Tag(name = "商家相关功能接口")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //商家的分页列表查询
    @Operation(summary = "查询商家列表")
    @GetMapping("/admin/basic/business/info/list")
    public Result<PageResult<BusinessVO>> pageList(BusinessQuery query)
            throws BusinessException {
        log.info("查询商家列表，入参:{}",query);
        PageResult<BusinessVO> result = businessService.SelectPageList(query);
        log.info("查询商家列表，出参:{}",result.getObjects());
        return new Result<>(result);
    }

    // 新增导出接口
    @Operation(summary = "导出全部商家数据")
    @GetMapping("/admin/basic/business/info/exportAll")
    public void exportAllBusiness(HttpServletResponse response) throws BusinessException {
        log.info("导出全部商家数据");
        try {
            businessService.exportAllBusiness(response);
        } catch (Exception e) {
            log.error("导出商家数据失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }

    @Operation(summary = "新增商家")
    @PostMapping("/admin/basic/business/info/save")
    public Result<Void> save(@RequestBody BusinessParam param) throws BusinessException {
        log.info("保存商家信息，入参:{}",param);
        businessService.save(param);
        return Result.success();
    }

    @Operation(summary = "商家详情")
    @GetMapping("/admin/basic/business/info/detail/{id}")
    public Result<BusinessStoreVO> busiStoreDetail(@PathVariable("id") Integer businessId)
            throws BusinessException {
        log.info("查询商家详情，入参:{}",businessId);
        BusinessStoreVO result = businessService.busiStoreDetail(businessId);
        return new Result<>(result);
    }

    @PutMapping("/admin/basic/business/info/updateStatus")
    @Operation(summary = "更新商家状态")
    public Result<Void> updateStatus(@RequestBody BusinessParam param) throws BusinessException {
        log.info("更新商家状态，入参:{}",param);
        businessService.updateStatus(param);
        return Result.success();
    }
}